#include <bits/stdc++.h>

//#pragma GCC optimize(2)
#define For(x, y, z) for (int x = y; x <= z; ++x)
#define Rof(x, y, z) for (int x = y; x >= z; --x)
#define Eor(u) for (int i = head[u]; i; i = nxt[i])
#define SZ(x) (int(x.size()))
#define pb push_back

using namespace std;
using i64 = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using lb = long double;

// char buf[(1<<21)+5],*p1,*p2;
// #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)

inline int read() {
	int x = 0, f = 0; char ch = getchar();
	while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
	while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
	return f ? -x : x;
}
int __stk[128], __tp;
inline void put(i64 x) {
	if (x < 0) putchar('-'), x = -x;
	do { __stk[++__tp] = x % 10, x /= 10; } while (x);
	while (__tp) putchar(__stk[__tp--] + '0');
}

const int mod = 998244353;
inline int ksm(int x, int y, int res = 1) {
	for ( ; y; y >>= 1, x = 1ll * x * x % mod)
		(y & 1) && (res = 1ll * res * x % mod);
	return res;
}
inline int inv(int x) { return ksm(x, mod - 2); }
inline int gcd(int a, int b) { if (b) while ((a %= b) && (b %= a)); return a | b; }
inline void add(int &x, int y) { (x += y) >= mod && (x -= mod); }
inline void Min(int &x, int y) { (y < x) && (x = y); }
inline void Max(int &x, int y) { (y > x) && (x = y); }

const int N = 1600;

int n, m;
char s[N];

namespace AC {
    struct node {
        lb w; int cnt; 
        int ch[10], fail;
    }t[N * 10];

    int num = 0, root;
    void New(int &x) {
        x = ++num, t[x].w = t[x].cnt = t[x].fail = 0;
        For(i, 0, 9) t[x].ch[i] = -1;
    }
    void insert(string s, int val) {
        int p = root;
        for (auto c : s) {
            if (t[p].ch[c - '0'] == -1) New(t[p].ch[c - '0']);
            p = t[p].ch[c - '0'];
        } ++t[p].cnt, t[p].w += logl(val);
    }
    queue<int> q;
    int f[N], tot;
    void build() {
        q.push(root); For(i, 0, 9) t[0].ch[i] = root;
        while (!q.empty()) {
            int u = q.front(), Fail = t[u].fail; q.pop();
            f[++tot] = u;
            // cout << "Error:" << u << " " << Fail<< "\n";
            For(i, 0, 9) {  
                int v = t[u].ch[i]; 
                if (v != -1) {
                    // cout <<v<<' '<<Fail <<" "<<t[Fail].ch[i]<<'\n';
                    t[v].w += t[u].w, t[v].cnt += t[u].cnt;
                    t[v].fail = t[Fail].ch[i], q.push(v);
                } else t[u].ch[i] = t[Fail].ch[i];
            }
            // cout << '\n';
        }
        For(i, 1, num) cout << t[i].w << " "<<t[i].cnt<<'\n';
        // cout << '\n';
    }
    
    void out() {
        For(i, 0, num) {
            cout << "Error:"<<i<<" "<<t[i].fail<<'\n';
            For(j, 0, 9) cout << t[i].ch[j] <<" ";
            cout<<'\n';
        }
        cout << '\n';

    }
}

lb dp[N][N][2];
pii lst[N][N][2];

bool check(lb k) {
    For(i, 0, n) For(j, 1, AC::num) dp[i][j][0] = dp[i][j][1] = -1e9;
    dp[0][1][0] = 0;
    For(i, 0, n) {
        For(j, 1, AC::num) {
            dp[i][j][0] += AC::t[j].w - AC::t[j].cnt * k;
            dp[i][j][1] += AC::t[j].w - AC::t[j].cnt * k;
            if (AC::t[j].cnt) 
                dp[i][j][1] = max(dp[i][j][1], dp[i][j][0]);
            
            cout << "Error:"<<i<<" "<<j<<" "<<dp[i][j]<<"\n";
            For(op, 0, 1) {
                if (s[i + 1] == '.') {
                    For(o, 0, 9) dp[i + 1][AC::t[j].ch[o]][op] = 
                        max(dp[i + 1][AC::t[j].ch[o]][op], dp[i][j][op]);
                } else {
                    For(o, s[i + 1] - '0', s[i + 1] - '0') 
                        dp[i + 1][AC::t[j].ch[o]][op] = max(dp[i + 1][AC::t[j].ch[o]][op], dp[i][j][op]);
                }
            }
            
        }
    }
    For(i, 1, AC::num) if (dp[n][i][1] >= 0) return 1;
    return 0; 
}
int res[N], len;

void work(lb k) {
    For(i, 0, n) For(j, 1, AC::num) dp[i][j][0] = dp[i][j][1] = -1e9;
    dp[0][1][0] = 0;
    For(i, 0, n) For(j, 1, AC::num) {
        dp[i][j][0] += AC::t[j].w - AC::t[j].cnt * k;
        dp[i][j][1] += AC::t[j].w - AC::t[j].cnt * k;
        if (AC::t[j].cnt) {
            if (dp[i][j][1] > dp[i][j][0]) {
                dp[i][j][1] = dp[i][j][0];
                lst[i][j][1] = pii(j, 0);
            }
        }
            

        For(op, 0, 1) {
            if (s[i + 1] == '.') 
            For(o, 0, 9) if (dp[i][j][op] > dp[i + 1][AC::t[j].ch[o]][op])
                dp[i + 1][AC::t[j].ch[o]][op] = dp[i][j][op], lst[i + 1][AC::t[j].ch[o]][op] = pii(j, op);
            else For(o, s[i + 1] - '0', s[i + 1] - '0') if (dp[i][j] > dp[i + 1][AC::t[j].ch[o]])
                dp[i + 1][AC::t[j].ch[o]][op] = dp[i][j][op], lst[i + 1][AC::t[j].ch[o]][op] = pii(j, op);
        }
    }
    pii o = pii(0, 0); For(i, 1, AC::num) if (dp[n][i][1] >= 0) { o = {i, 1}; break; }

    Rof(i, n, 1) For(j, 0, 9) if (AC::t[lst[i][o.first][o.second].first].ch[j] == o.first) 
        { res[i] = j, o = lst[i][o.first][o.second]; break; }
}
signed main() {
	freopen("magic.in", "r", stdin);
	freopen("magic.out", "w", stdout);
    n = read(), m = read(), scanf("%s", s + 1), AC::New(AC::root);
    For(i, 1, m) { string s; cin >> s; int w = read(); AC::insert(s, w); }

    // AC::out();

    AC::build(); lb l = 0, r = 1e8, ans = 0;

    // AC::out();

    cout << check(1) << '\n';

    // while (r - l > 1e-7) {
    //     lb mid = (l + r) / 2.0;
    //     if (check(mid)) 
    //         l = mid, ans = mid;
    //     else r = mid;
    // }  
    // cout << ans << '\n';
    // work(ans); For(i, 1, n) put(res[i]);
	return 0;
}